package com.wandafilm.app.jetpack.frame.ui.main

import android.content.Intent
import android.util.ArrayMap
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.jeremyliao.liveeventbus.LiveEventBus
import com.kotlin.android.core.BaseVMActivity
import com.kotlin.android.ktx.ext.KEY_EXTRA
import com.kotlin.android.ktx.ext.KEY_SCREEN_HEIGHT
import com.kotlin.android.ktx.ext.KEY_SCREEN_WIDTH
import com.kotlin.android.ktx.utils.LogUtils
import com.kotlin.android.liveevent.LOGIN_SUCCESS
import com.kotlin.android.liveevent.event.LoginState
import com.kotlin.android.router.factory.ProviderFactory
import com.kotlin.android.router.interceptor.LoginNavigationCallbackImpl
import com.kotlin.android.router.path.RouterActivityPath
import com.kotlin.android.router.path.RouterProviderPath
import com.kotlin.android.router.provider.simple.ISimpleProvider
import com.wandafilm.app.R
import kotlinx.android.synthetic.main.activity_main.*

@Route(path = RouterActivityPath.Main.PAGER_MAIN)
class MainActivity : BaseVMActivity<MainViewModel>() {
    private val createProvider = ProviderFactory
        .getInstance<ISimpleProvider>(RouterProviderPath.Provider.PROVIDER_SIMPLE)
    override fun getLayoutResId(): Int = R.layout.activity_main

    override fun initVM(): MainViewModel = viewModels<MainViewModel>().value

    override fun initView() {
        simpleActivity?.setOnClickListener {
            val arrayMapOf = ArrayMap<String, Any>()
            arrayMapOf[KEY_SCREEN_HEIGHT] = "120"
            arrayMapOf[KEY_SCREEN_WIDTH] = 150
            createProvider?.startSimpleMVVM(
                activity = this,
                map = arrayMapOf,
                callback = LoginNavigationCallbackImpl()
            )
        }
        simpleActivity2?.setOnClickListener {
            val arrayMapOf = ArrayMap<String, Any>()
            arrayMapOf[KEY_SCREEN_HEIGHT] = "100"
            arrayMapOf[KEY_SCREEN_WIDTH] = 50
            arrayMapOf[KEY_EXTRA] = LoginState(isLogin = true)
            createProvider?.startSimpleMVVM2Result(this, arrayMapOf, 123)
        }
        batchActivity?.setOnClickListener {
            createProvider?.startBatchMVVM()
        }
        permissionVMActivity?.setOnClickListener {
            createProvider?.startPermissionMVVM()
        }
    }

    override fun initData() {
        mViewModel?.initGateway()
    }

    override fun startObserve() {
        LiveEventBus.get(LOGIN_SUCCESS, LoginState::class.java)//登录成功的事件监听
            .observe(this, Observer {
                LogUtils.d("${it.isLogin}")
            })
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        LogUtils.e("$requestCode    $resultCode")
    }
}
